#!/usr/bin/python3

import cv2
import time, sys
import pickle
import Camera

def nothing(x):
    pass

if __name__ == "__main__":
    print("ANY change to camera parameters is IRREVERSIBLE")
    print("Make sure you THINK TWICE before modification")
    sure = input("Continue?")
    if not (sure == "Y" or sure == "y"):
        exit()
    
    cam_idx_list = []
    with open("../configs/cam_idx.bin", 'rb') as cam_idx_file:
        cam_idx_list = pickle.load(cam_idx_file)
    cp = Camera.get_capture()
    cv2.namedWindow("cam", cv2.WINDOW_AUTOSIZE)
    # Remember to create a window before adding any trackbar

    cv2.createTrackbar("AUTO_WB",    "cam", int(cp.get(cv2.CAP_PROP_AUTO_WB)),      1, nothing)
    cv2.createTrackbar("SATURATION", "cam", int(cp.get(cv2.CAP_PROP_SATURATION)), 255, nothing)
    cv2.createTrackbar("BRIGHTNESS", "cam", int(cp.get(cv2.CAP_PROP_BRIGHTNESS)), 255, nothing)
    cv2.createTrackbar("HUE",        "cam", int(cp.get(cv2.CAP_PROP_HUE)),        255, nothing)
    cv2.createTrackbar("CONTRAST",   "cam", int(cp.get(cv2.CAP_PROP_CONTRAST)),   255, nothing)
    # cv2.createTrackbar("GAIN", "cam", int(cp.get(cv2.CAP_PROP_GAIN)), 255, nothing)
    # Our camera seems to be incompatible with GAIN argument
    cv2.createTrackbar("GAMMA",      "cam", int(cp.get(cv2.CAP_PROP_GAMMA)*100), 1000, nothing)
    # Gamma value seems to be a exponent. We need to tune it in a much more precise way
    
    idx = 0
    while True:
        i = cam_idx_list[idx]
        idx += 1
        idx %= 4
        print(i)
        cp.open(i)
        if not cp.isOpened():
            continue

        print(cp.get(cv2.CAP_PROP_AUTO_WB))
        print(cp.get(cv2.CAP_PROP_SATURATION))
        print(cp.get(cv2.CAP_PROP_BRIGHTNESS))
        print(cp.get(cv2.CAP_PROP_HUE))
        print(cp.get(cv2.CAP_PROP_CONTRAST))
        print(cp.get(cv2.CAP_PROP_GAMMA))
        cv2.setTrackbarPos("AUTO_WB",    "cam", int(cp.get(cv2.CAP_PROP_AUTO_WB)))
        cv2.setTrackbarPos("SATURATION", "cam", int(cp.get(cv2.CAP_PROP_SATURATION)))
        cv2.setTrackbarPos("BRIGHTNESS", "cam", int(cp.get(cv2.CAP_PROP_BRIGHTNESS)))
        cv2.setTrackbarPos("HUE",        "cam", int(cp.get(cv2.CAP_PROP_HUE)))
        cv2.setTrackbarPos("CONTRAST",   "cam", int(cp.get(cv2.CAP_PROP_CONTRAST)))
        cv2.setTrackbarPos("GAMMA",      "cam", int(cp.get(cv2.CAP_PROP_GAMMA)*100))
        while 1:
            _, frame = cp.read()
            if not _:
                continue

            cp.set(cv2.CAP_PROP_AUTO_WB,    cv2.getTrackbarPos("AUTO_WB", "cam"))
            cp.set(cv2.CAP_PROP_SATURATION, cv2.getTrackbarPos("SATURATION", "cam"))
            cp.set(cv2.CAP_PROP_BRIGHTNESS, cv2.getTrackbarPos("BRIGHTNESS", "cam"))
            cp.set(cv2.CAP_PROP_HUE,        cv2.getTrackbarPos("HUE", "cam"))
            cp.set(cv2.CAP_PROP_CONTRAST,   cv2.getTrackbarPos("CONTRAST", "cam"))
            cp.set(cv2.CAP_PROP_GAMMA,float(cv2.getTrackbarPos("GAMMA", "cam")) / 100. )
    
            cv2.imshow("cam", frame)
            key = cv2.waitKey(1)
            if key == ord('q'):
                exit()
            if key == ord('c'):
                break
